package com.dom.invoapp.modular.provider;

import com.baomidou.mybatisplus.plugins.Page;
import com.dom.api.purc.PurcCredApi;
import com.dom.common.model.vo.apply.*;
import com.dom.common.utils.BeanUtil;
import com.dom.invoapp.dao.entity.TpPurcCredDetailFlow;
import com.dom.invoapp.dao.entity.TpPurcCredFlow;
import com.dom.invoapp.modular.service.CommonService;
import com.dom.invoapp.modular.service.PurcCredService;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 采购单生产服务者
 * @Author xllei
 * @Date 2018/12/9 3:41 AM
 * @Version 1.0
 **/
@RestController
@Slf4j
public class PurcCredProvider extends AbstractProvider implements PurcCredApi {
    @Autowired
    private PurcCredService purcCredService;
    @Autowired
    private CommonService commonService;

    @Override
    public P059A getPurcCredPager(@RequestBody P059R request) {
        Page<TpPurcCredFlow> pager = purcCredService.findByPager(request);

        //获取机构列表、供货商列表、仓库列表
        Map<Integer, String> mapOrganName = commonService.getOrganList(request.getMemCode());
        Map<Integer, String> mapSuppName = commonService.getSuppList(request.getMemCode());
        Map<Integer, String> mapStorName = commonService.getStorList(request.getMemCode());
        Map<Integer, String> mapUserName = commonService.getUserList(request.getMemCode());

        List<CredInfo> credInfos = new ArrayList<>();
        pager.getRecords().forEach(record -> {
            CredInfo info = new CredInfo();
            BeanUtil.copyPropertiesIgnoreNull(record, info);
            info.setCredId(record.getId());
            info.setMakerId(record.getDocuMakerId());
            info.setMakeDate(DateUtil.format(record.getMakeDate(), "yyyy-MM-dd HH:mm:ss"));
            info.setExchDate(DateUtil.format(record.getExchDate(), "yyyy-MM-dd"));
            if (ObjectUtil.isNotNull(record.getCancelTime())) {
                info.setCancelTime(DateUtil.format(record.getCancelTime(), "yyyy-MM-dd HH:mm:ss"));
            }

            //赋值机构名称
            info.setOrganName(mapOrganName.get(record.getOrganCode()));

            //赋值仓库名称
            info.setStorName(mapStorName.get(record.getStorCode()));

            //赋值供货商名称
            info.setAbbrName(mapSuppName.get(record.getSuppCode()));

            info.setMakerName(mapUserName.get(record.getDocuMakerId()));
            credInfos.add(info);
        });

        P059A response = new P059A();
        response.setTotalNum((int) pager.getTotal());
        response.setCredData(credInfos);
        return response;
    }

    @Override
    public P050A addPurcCred(@RequestBody P050R request) {
        Integer credId = purcCredService.insertPurcCred(request);
        P050A response = new P050A();
        response.setCredId(credId);
        return response;
    }

    @Override
    public P053A getCredParticulars(@RequestBody P053R request) {
        //查询订单信息
        TpPurcCredFlow credFlow = purcCredService.findPurcCredInfo(request);

        //查询详情信息
        List<TpPurcCredDetailFlow> detailFlowList = purcCredService.findCredListByCredId(credFlow.getId(), request);
        List<CredDetailInfo> credDetailInfos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(detailFlowList)) {
            detailFlowList.forEach(detailFlow -> {
                CredDetailInfo detailInfo = new CredDetailInfo();
                BeanUtil.copyPropertiesIgnoreNull(detailFlow, detailInfo);
                detailInfo.setCredDetailId(detailFlow.getId());

                credDetailInfos.add(detailInfo);
            });
        }

        P053A response = buildP053A(credFlow);
        response.setTotalNum(credDetailInfos.size());
        response.setCredDetailData(credDetailInfos);

        return response;
    }

    private P053A buildP053A(@RequestBody TpPurcCredFlow purcCredFlow) {
        P053A response = new P053A();
        BeanUtil.copyPropertiesIgnoreNull(purcCredFlow, response);
        response.setCredId(purcCredFlow.getId());
        response.setBillAbstract(purcCredFlow.getDigest());
        return response;
    }

    @Override
    public P051A delCredPurcInfo(@RequestBody P051R request) {
        Integer credId = purcCredService.delCredPurcInfo(request);
        P051A response = new P051A();
        response.setCredId(credId);
        return response;
    }

    @SuppressWarnings("warning")
    @Override
    public P069A getPurcCredDetailList(@RequestBody P069R request) {
        P069A response = new P069A();
        List<CredDetailInfo> records = new ArrayList<>();
        List<TpPurcCredDetailFlow> purcCredDetailFlows = new ArrayList<>();

        response.setCredId(request.getCredId());

        // 分页查询
        if (request.isPagination()) {
            Page pager = purcCredService.findPurcCredDetailPager(request);
            purcCredDetailFlows = pager.getRecords();
        } else {//全量数据查询
            purcCredDetailFlows = purcCredService.findPurcCredDetailList(request);
        }

        purcCredDetailFlows.forEach(entity -> {
            CredDetailInfo info = new CredDetailInfo();
            BeanUtil.copyPropertiesIgnoreNull(entity, info);
            info.setCredDetailId(entity.getId());
            info.setSeqNo(records.size()+1);

            records.add(info);
        });

        response.setTotalNum(records.size());
        response.setCredDetailData(records);
        return response;
    }

    @Override
    public P052A editPurcCred(@RequestBody P052R request) {
        //修改订单及明细
        Integer credId = purcCredService.updatePurcCredAndDetail(request);

        P052A response = new P052A();
        response.setCredId(credId);
        return response;
    }
}
